package org.foxas.tween
{
	import flash.display.Sprite;
	import flash.events.Event;

	public class InertiaTween extends Sprite
	{
		private var friction:Number = 5;//friction
		private var directX:int = 0;//speed's direction
		private var directY:int = 0;//speed's direction
	
		private var valueX:Number = 0;
		private var speedX:Number = 0;//friction
		private var maxX:Number = 0;//max 
		private var minX:Number = 0;//min
		
		private var valueY:Number = 0;
		private var speedY:Number = 0;//friction
		private var maxY:Number = 0;//max 
		private var minY:Number = 0;//min
	
		public var listener_frame:Function;
		public var listener_complete:Function;
		public var listener_out:Function;
		
		public var isRun:Boolean = false;//
		public var Config:Object = new Object();
		public function InertiaTween()
		{
			super();
			Config.collide=2;
		}
		public function fix(_valueX:Number,_speedX:Number,_valueY:Number,_speedY:Number,_maxX:Number=9999,_maxY:Number=9999,_minX:Number=0,_minY:Number=0):void{
			valueX = _valueX;
			speedX = _speedX;
			maxX = _maxX;
			minX = _minX;
			valueY = _valueY;
			speedY = _speedY;
			maxY = _maxY;
			minY = _minY;		
		}
		public function start():void{
			if(speedX>0){
				//speedX = Math.max(80,speedX);
				directX =1;
			}else if(speedX<0){
				//speedX = Math.min(-80,speedX);
				directX = -1;
			}else{
				directX = 0;
			}
			if(speedY>0){
				//speedX = Math.max(80,speedX);
				directY =1;
			}else if(speedY<0){
				//speedX = Math.min(-80,speedX);
				directY = -1;
			}else{
				directY = 0;
			}
			isRun = true;
			this.addEventListener(Event.ENTER_FRAME,frameHandle);
		}
		public function stop():void{
			isRun = false;
			this.removeEventListener(Event.ENTER_FRAME,frameHandle);
		}
		private function frameHandle(_e:Event):void{
			if(directX==1){
				if(speedX<=0){
					speedX = 0;
				}else{
					valueX +=speedX;
					speedX -= friction;
//					trace('tween',mc[prop],speedX);
					if(valueX>=maxX){
//						if(Config.collide==3){
//							speedX*=-1;
//							directX*=-1;
//						}else if(Config.collide==2){
//							speedX*=-1;
//							speedX*=0.6;
//							directX*=-1;
//						}else if(Config.collide==1){
//							speedX*=-1;
//							speedX/=4;
//							directX*=-1;
//						}else{
//							speedX = 0;
//							directX = 0;
//						}
					}
				}
			}else if(directX==-1){
				if(speedX>=0){
					speedX = 0;
				}else{
					valueX +=speedX;
					speedX += friction;
//					trace('tween',mc[prop],speedX);
					if(valueX<=minX){
//						if(Config.collide==3){
//							speedX*=-1;
//							directX*=-1;						
//						}else if(Config.collide==2){
//							speedX*=-1;
//							speedX*=0.6;
//							directX*=-1;
//						}else if(Config.collide==1){
//							speedX*=-1;
//							speedX/=4;
//							directX*=-1;
//						}else{
//							speedX = 0;
//							directX = 0;
//						}
					}
				}				
			}else{
				speedX = 0;
			}
			if(directY==1){
				if(speedY<=0){
					speedY = 0;
				}else{
					valueY +=speedY;
					speedY -= friction;
//					trace('tween',mc[prop],speedY);
					if(valueY>=maxY){
//						if(Config.collide==3){
//							speedY*=-1;
//							directY*=-1;						
//						}else if(Config.collide==2){
//							speedY*=-1;
//							speedY*=0.6;
//							directY*=-1;
//						}else if(Config.collide==1){
//							speedY*=-1;
//							speedY/=4;
//							directY*=-1;
//						}else{
//							speedY = 0;
//							directY = 0;
//						}
					}
				}
			}else if(directY==-1){
				if(speedY>=0){
					speedY = 0;
				}else{
					valueY +=speedY;
					speedY += friction;
//					trace('tween',mc[prop],speedY);
					if(valueY<=minY){
//						if(Config.collide==3){
//							speedY*=-1;
//							directY*=-1;						
//						}else if(Config.collide==2){
//							speedY*=-1;
//							speedY*=0.6;
//							directY*=-1;
//						}else if(Config.collide==1){
//							speedY*=-1;
//							speedY/=4;
//							directY*=-1;
//						}else{
//							speedY = 0;
//							directY = 0;
//						}
					}
				}				
			}else{
				speedY = 0;
			}
			//
			if(speedX==0 && speedY==0){
				complete();
			}else{
				if(this.listener_frame!=null){
					this.listener_frame(this.valueX,this.valueY);
				}				
			}
//			trace(valueX,valueY);
//			this.valueX = F3math.area(maxX,this.valueX,minX);
//			this.valueY = F3math.area(maxY,this.valueY,minY);

		}
		public function complete():void{
			stop();
			if(this.listener_complete!=null){
				listener_complete();				
			}
		}
	}
}